package com.acwing.partition12;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/1/8 14:43
 */
public class AC1128信使floyd {

    private static final int N = 110, M = 2 * 210, INF = 0x3f3f3f3f;
    private static int[][] graph = new int[N][N];
    private static int index = 0, n = 0, m = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) graph[i][j] = INF;
        }
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split("\\s+");
            int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]), w = Integer.parseInt(s[2]);
            graph[u][v] = graph[v][u] = w;
        }
        writer.write(floyd(1) + "\n");
        writer.flush();
    }

    private static int floyd(int start) {
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    graph[i][j] = Math.min(graph[i][j], graph[i][k] + graph[k][j]);
                }
            }
        }
        int answer = 0;
        for (int i = 1; i <= n; i++) {
            if (i == start) continue;
            if (graph[start][i] == INF) {
                answer = -1;
                break;
            }
            answer = Math.max(answer, graph[start][i]);
        }
        return answer;
    }
}
